Conditions | 1 |
Paths | 8 |
Total Lines | 67 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | const app = angular.module('rv2Flash', []); |
||
141 | this.$get = ['$rootScope', '$interval', function($rootScope, $interval) { |
||
142 | const dataFactory = {}; |
||
143 | let counter = 0; |
||
144 | |||
145 | dataFactory.setTimeout = this.setTimeout; |
||
146 | dataFactory.setShowClose = this.setShowClose; |
||
147 | dataFactory.setOnDismiss = this.setOnDismiss; |
||
148 | dataFactory.config = defaultConfig; |
||
149 | |||
150 | dataFactory.create = function(type, text, timeout, config, showClose) { |
||
151 | if ($rootScope.flashes.length === 1 && defaultConfig.autoDismiss) { |
||
152 | dataFactory.dismiss($rootScope.flashes[0].id); |
||
153 | } |
||
154 | if (!text) return false; |
||
155 | let $this, flash; |
||
156 | $this = this; |
||
157 | flash = { |
||
158 | type: type, |
||
159 | text: text, |
||
160 | config: config, |
||
161 | id: counter++ |
||
162 | }; |
||
163 | flash.showClose = |
||
164 | typeof showClose !== 'undefined' ? |
||
165 | showClose : defaultConfig.showClose; |
||
166 | if (defaultConfig.timeout && typeof timeout === 'undefined') { |
||
167 | flash.timeout = defaultConfig.timeout; |
||
168 | } |
||
169 | else if (timeout) { |
||
170 | flash.timeout = timeout; |
||
171 | } |
||
172 | $rootScope.flashes.push(flash); |
||
173 | if (flash.timeout) { |
||
174 | flash.timeoutObj = $interval(function() { |
||
175 | $this.dismiss(flash.id); |
||
176 | }, flash.timeout, 1); |
||
177 | } |
||
178 | return flash.id; |
||
179 | }; |
||
180 | dataFactory.pause = function(index) { |
||
181 | if ($rootScope.flashes[index].timeoutObj) { |
||
182 | $interval.cancel($rootScope.flashes[index].timeoutObj); |
||
183 | } |
||
184 | }; |
||
185 | dataFactory.dismiss = function(id) { |
||
186 | const index = findIndexById(id); |
||
187 | if (index !== -1) { |
||
188 | const flash = $rootScope.flashes[index]; |
||
189 | dataFactory.pause(index); |
||
190 | $rootScope.flashes.splice(index, 1); |
||
191 | if (typeof defaultConfig.onDismiss === 'function') { |
||
192 | defaultConfig.onDismiss(flash); |
||
193 | } |
||
194 | } |
||
195 | }; |
||
196 | dataFactory.clear = function() { |
||
197 | while ($rootScope.flashes.length > 0) { |
||
198 | dataFactory.dismiss($rootScope.flashes[0].id); |
||
199 | } |
||
200 | }; |
||
201 | dataFactory.reset = dataFactory.clear; |
||
202 | function findIndexById(id) { |
||
203 | return $rootScope.flashes.map((flash) => flash.id).indexOf(id); |
||
204 | } |
||
205 | |||
206 | return dataFactory; |
||
207 | }]; |
||
208 | }); |
||
209 |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.